<template>
<view>
<form @submit="release" reportSubmit="true">
    <view class="modle" style="border-bottom:0">
    配件图片
    <text class="must_red">*</text>
    </view>
    <view class="shangchuan_view" v-if="partImg!=''">
        <image @tap="delImg" class="shangchuan_Img_close" src="/static/pages/image/tupiangb_btn.png"></image>
        <image @tap="showimg" class="shangchuan_Img" :src="partImg"></image>
    </view>
    <view class="shangchuan_view" v-if="partImg==''">
        <image @tap="upcarsimgs" class="shangchuan_Img" src="/static/pages/image/tianjiantp_btn.png"></image>
    </view>
    <view class="modle">
    配件名称
    <text class="must_red">*</text>
        <input class="modle_right" name="part_name" placeholder="请输入配件名称" placeholderStyle="color: #9295A8;" type="text" value>
    </view>
    <view class="modle">
    车辆类型
    <text class="must_red">*</text>
        <picker @change="selectCarType" class="modle_right" :range="carTypeList" rangeKey="name" :value="index">
            <view>
        {{carTypeName}}
      </view>
        </picker>
    </view>
    <view class="modle">
    年份
    <text class="must_red">*</text>
        <input class="modle_right" name="car_year" placeholder="请输入年份" placeholderStyle="color: #9295A8;" style="width:350rpx;" type="digit" :value="defult_value">
    </view>
    <view class="modle">
    配件类型
    <text class="must_red">*</text>
        <picker @change="selectPartType" class="modle_right" :range="partTypeList" rangeKey="name" :value="index">
            <view>
        {{partTypeName}}
      </view>
        </picker>
    </view>
    <view class="modle">
        <input class="modle_right btm_input" name="user_tel" placeholder placeholderStyle="color: #9295A8;" type="number" :value="tel">
    联系电话
    <text class="must_red">*</text>
        <button @getphonenumber="getPhoneNumber" class="modle_btn" open-type="getPhoneNumber">电话授权</button>
    </view>
    <button class="bufa_btn" form-type="submit">提交</button>
    <view class="jinggao">提交后，后台客服会根据提交信息进行报价。</view>
</form>
<!--<import src="/pages/alert/index.wxml"></import>-->
<block data-type="template" data-is="alert" data-attr="isShow:isShow">
    <view class="wx_dialog_container" :hidden="!isShow">
        <view class="wx-alert-mask"></view>
        <view class="wx-alert-dialog">
            <view class="wx-alert-dialog-title">尊敬的用户</view>
            <view class="wx-alert-dialog-content">尚未获取到您的用户信息，较多功能无法体验，请允许获取用户信息</view>
            <view class="wx-alert-dialog-footer">
                <button @tap="hideDialog" class="wx-alert-dialog-btn">拒绝</button>
                <button @getuserinfo="updateUserInfo" class="wx-alert-dialog-btn" open-type="getUserInfo">允许</button>
            </view>
        </view>
    </view>
</block>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
      buttscan: !1,
      isShow: !1,
      carTypeId: 0,
      carTypeName: "请选择车型",
      carTypeList: [],
      partTypeId: 0,
      partTypeName: "请选择配件类型",
      partTypeList: [{
        id: 1,
        name: "原厂全新"
      }, {
        id: 2,
        name: "副厂全新"
      }, {
        id: 3,
        name: "原车拆件"
      }],
      tel: "",
      partImg: "",
      imgPath: "",
      defult_value: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (t) {
    var a = this;
    console.log(a.partTypeList), app.globalData.util.getUserInfo(function (t) {
      t.memberInfo || a.setData({
        isShow: !0
      });
    }), app.globalData.util.request({
      url: "entry/wxapp/Api",
      data: {
        m: "monai_market",
        r: "part.index.get_car_type"
      },
      success: function (t) {
        a.setData({
          carTypeList: t.data.data
        });
      }
    });
  },
  methods: {
    hideDialog: function () {
      this.setData({
        isShow: !this.isShow
      });
    },
    updateUserInfo: function (t) {
      var a = this;
      app.globalData.util.getUserInfo(function (t) {
        wx.setStorageSync("uid", t.memberInfo.uid), a.hideDialog();
      }, t.detail);
    },
    selectCarType: function (t) {
      console.log(t), console.log(t.detail.value);
      var a = this.carTypeList[t.detail.value];
      a && this.setData({
        carTypeId: a.id,
        carTypeName: a.name
      });
    },
    selectPartType: function (t) {
      var a = this.partTypeList[t.detail.value];
      a && this.setData({
        partTypeId: a.id,
        partTypeName: a.name
      });
    },
    getPhoneNumber: function (t) {
      console.log(t);
      var a = this;
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          r: "part.index.get_tel",
          m: "monai_market",
          iv: t.detail.iv,
          encryptedData: t.detail.encryptedData
        },
        success: function (t) {
          a.setData({
            tel: t.data.data
          });
        }
      });
    },
    release: function (t) {
      var a = this;
      if (a.buttscan) return !1;
      a.setData({
        buttscan: !0
      });
      var e = a.imgPath;
      if (!e) return app.globalData.util.message({
        title: "请上传配件图片",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var r = t.detail.value.part_name;
      if (!r) return app.globalData.util.message({
        title: "请输入配件名称",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var i = a.carTypeId;
      if (i <= 0) return app.globalData.util.message({
        title: "请选择车型",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var s = t.detail.value.car_year;
      if (!s) return app.globalData.util.message({
        title: "请输入年份",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var p = a.partTypeId;
      if (p <= 0) return app.globalData.util.message({
        title: "请选择配件类型",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var n = t.detail.value.user_tel;
      if (!s) return app.globalData.util.message({
        title: "请输入手机号码",
        type: "error"
      }), a.setData({
        buttscan: !1
      }), !1;
      var u = {
        r: "part.index.order_add",
        m: "monai_market",
        uid: app.globalData.getuid(),
        part_img: e,
        part_name: r,
        car_type: i,
        car_year: s,
        part_type: p,
        user_tel: n
      };
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: u,
        success: function (t) {
          wx.showModal({
            content: "提交成功",
            showCancel: !1,
            success: function (t) {
              a.setData({
                buttscan: !1
              }), wx.navigateBack();
            }
          });
        },
        fail: function (t) {
          a.setData({
            buttscan: !1
          });
        }
      });
    },
    upcarsimgs: function () {
      var a = this;
      app.globalData.upimgs({
        scannums: 1,
        filename: "parts",
        success: function (t) {
          console.log(t), a.setData({
            partImg: t[0].all,
            imgPath: t[0].short
          });
        }
      });
    },
    showimg: function (t) {
      wx.previewImage({
        current: this.partImg,
        urls: [this.partImg]
      });
    },
    delImg: function (t) {
      app.globalData.util.request({
        url: "entry/wxapp/Api",
        data: {
          m: "monai_market",
          uid: app.globalData.getuid(),
          imgurl: this.partImg,
          r: "Upimg.delImg"
        }
      }), this.setData({
        partImg: "",
        imgPath: ""
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>